import {
    Controller,
    Get,
    Post,
    Body,
    Patch,
    Param,
    Delete,
    UploadedFile,
    UseInterceptors,
    HttpCode, Req, Query
} from "@nestjs/common";
import { OssService } from "./oss.service";
import { CreateOssDto } from "./dto/create-oss.dto";
import { UpdateOssDto } from "./dto/update-oss.dto";
import { ApiBearerAuth, ApiBody, ApiConsumes, ApiOperation, ApiTags } from "@nestjs/swagger";
import { FileInterceptor } from "@nestjs/platform-express";
import { FindAllDto } from "@/modules/oss/dto/find-all.dto";

@ApiBearerAuth()
@ApiTags("文件存储")
@Controller("oss")
export class OssController {
    constructor(private readonly ossService: OssService) {
    }
    
    @Post("upload")
    @ApiOperation({ summary: "文件上传,返回 url 地址" })
    @ApiConsumes("multipart/form-data")
    @ApiBody({
        schema: {
            type: "object",
            properties: {
                file: {
                    description: "文件",
                    type: "string",
                    format: "binary"
                },
                business: {
                    description: "上传文件描述，可以是纯字符串，也可以是JSON字符串",
                    type: "string",
                    format: "text"
                },
                folderPath: {
                    description: "文件夹地址",
                    type: "string",
                    format: "text",
                    example: "wenjianja/001"
                }
            }
        }
    })
    @UseInterceptors(FileInterceptor("file"))
    async uploadFile(@UploadedFile() file, @Body() params: { business: string, folderPath: string }, @Req() req) {
        return this.ossService.create(file, params.business || "", params.folderPath || "", req.user);
    }
    
    @Get("createFolder")
    @ApiOperation({ summary: "创建文件夹" })
    @ApiBody({
        schema: {
            properties: {
                filePath: {
                    description: "文件夹地址",
                    type: "string",
                    format: "text",
                    example: "/wenjianja/001"
                }
            }
        }
    })
    createFolder(@Query() dto) {
        return this.ossService.createFolder(dto.folderPath);
    }
    
    @Get()
    @ApiOperation({ summary: "获取文件列表" })
    findAll(@Query() findAllDto: FindAllDto) {
        return this.ossService.findAll(findAllDto.folderPath);
    }
    
    @Get(":id")
    findOne(@Param("id") id: string) {
        return this.ossService.findOne(+id);
    }
    
    @Patch(":id")
    update(@Param("id") id: string, @Body() updateOssDto: UpdateOssDto) {
        return this.ossService.update(+id, updateOssDto);
    }
    
    @Delete()
    @ApiOperation({ summary: "删除文件" })
    @ApiBody({
        schema: {
            type: "object",
            properties: {
                filePath: {
                    description: "文件地址",
                    type: "string",
                    format: "text",
                    example: "./upload//wenjianja/001/6ed0e52285a04f65a33b58973e27eb80.png"
                }
            }
        }
    })
    remove(@Query() dto) {
        return this.ossService.remove(dto.filePath);
    }
}
